﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace CardPlay.Model.GameModel
{
    public static class StandardPileNames
    {
        public const string Stock = "stock";
        public const string Waste = "waste";
        public const string Reserve = "reserve";
        public const string Tableau = "tableau";
        public const string Foundation = "foundation";

        public const string PrefixForFoundation = Foundation + ".";
        public const string PrefixForReserve = Reserve + ".";
        public const string PrefixForTableau = Tableau + ".";

        public static IEnumerable<string> Foundations
        {
            get { return PrefixedWith(PrefixForFoundation); }
        }

        public static IEnumerable<string> Tableaus
        {
            get { return PrefixedWith(PrefixForTableau); }
        }

        public static IEnumerable<string> Reserves
        {
            get { return PrefixedWith(PrefixForReserve); }
        }

        public static IEnumerable<string> PrefixedWith(string prefix)
        {
            return Enumerable.Range(1, 128).Select(n => prefix + n);
        }

        public static string RangePatternForFoundations(int start, int count)
        {
            return RangePatternFor(PrefixForFoundation, start, count);
        }

        public static string RangePatternFor(string prefix, int start, int count)
        {
            return string.Format("{0}[{1}-{2}]", prefix, start, start + count - 1);
        }

        public static int GetOrdinal(this PartLocation location)
        {
            return GetSubscriptFrom(location.Path);
        }

        public static int GetOrdinal(this CardPile pile)
        {
            return GetSubscriptFrom(pile.Name);
        }

        private static int GetSubscriptFrom(string s)
        {
            return Int32.Parse(Regex.Match(s, @"[a-z]+\.(?<index>\d{1,2})").Groups["index"].Value);
        }
    }
}